package com.multipledatasource.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.multipledatasource.config.DataSourceContextHolder;
import com.multipledatasource.config.DataSourceEnum;
import com.multipledatasource.web.bean.OrderTab;
import com.multipledatasource.web.mapper.GoodsTabMapper;
import com.multipledatasource.web.mapper.OrderTabMapper;
import com.multipledatasource.web.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;


@Service
public class TestServiceImpl implements TestService {

    @Autowired
    OrderTabMapper orderTabMapper;
    @Autowired
    GoodsTabMapper goodsTabMapper;


    @Override
    public JSONObject addOrder(JSONObject jsonObject) {
        String goodsNumber =  jsonObject.getString("goodsNumber");
        String userNumber = jsonObject.getString("userNumber");
        Double payMoney = jsonObject.getDouble("payMoney");
        //新增订单
        OrderTab orderTab = new OrderTab();
        orderTab.setBuyCount(1);
        orderTab.setGoodsNumber(goodsNumber);
        orderTab.setOrderNumber(UUID.randomUUID().toString());
        orderTab.setPayMoney(payMoney);
        orderTab.setUserNumber(userNumber);
        DataSourceContextHolder.setDatabaseType(DataSourceEnum.ORDER);//设置订单数据源
        orderTabMapper.addOrder(orderTab);

        //商品数量减一
        DataSourceContextHolder.setDatabaseType(DataSourceEnum.GOODS);//设置商品数据源
        int identify = goodsTabMapper.goodsDecrement(goodsNumber);
        JSONObject json = new JSONObject();
        json.put("code",1);
        json.put("msg","成功");
        return json;
    }
}
